/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.lang.io;

import java.nio.ByteBuffer;

import leola.types.LeoNumber;



/**
 * Wrapper around {@link ByteBuffer}.
 * 
 * @author Tony
 *
 */
public class Buffer {
	
	/**
	 * Buffer
	 */
	private ByteBuffer buffer;
	
	/**
	 * @param size
	 */
	public Buffer(int size) {
		this.buffer = ByteBuffer.allocate(size);
	}

	
	public void putByte(LeoNumber n) {
		this.buffer.put( (byte)n.getNumber() );
	}
	
	public byte readByte() {
		return this.buffer.get();
	}
	
	/**
	 * @return reads the byte as a string
	 */
	public String readString() {
		return (char)this.buffer.get() + "";
	}
	
	public char readChar() {
		return (char)this.buffer.get();
	}
	
	public void putBuffer(Buffer buffer) {
		this.buffer.put(buffer.buffer);
	}
	
	public void putString(String str) {
		this.buffer.put(str.getBytes());
	}
	
	public int position() {
		return this.buffer.position();
	}
	
	public void setPosition(int pos) {
		this.buffer.position(pos);
	}
	
	public int capacity() {
		return this.buffer.capacity();
	}
	
	public void clear() {
		this.buffer.clear();
	}
	
	public void rewind() {
		this.buffer.rewind();
	}
	
	public int remaining() {
		return this.buffer.remaining();
	}
	
	public byte[] getArray() {
		return this.buffer.array();		
	}
	
	public int limit() {
		return this.buffer.limit();
	}
	
	public void setLimit(int limit) {
		this.buffer.limit(limit);
	}
	
	public void mark() {
		this.buffer.limit(this.buffer.position());
	}
}
